package com.solrtest.util;

import com.solrtest.doman.Order;
import org.apache.commons.lang3.ArrayUtils;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 简单实体 映射生成
 */
public class XmlMappingGeneration {

    public static void Generation(Class clazz, String pkName) {
        Field[] fields = ArrayUtils.addAll(clazz.getDeclaredFields(), clazz.getSuperclass().getDeclaredFields());
        StringBuilder sb = new StringBuilder();
        for (Field field : fields) {
            sb.append(fieldHandle(field.getName()));
        }
        System.out.println("=================创建[data-config.xml]  映射==============\n");
        System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        System.out.println("<dataConfig>");
        System.out.println("\t<dataSource driver=\"com.microsoft.sqlserver.jdbc.SQLServerDriver\" url=\"jdbc:sqlserver://192.168.1.8:1433;SelectMethod=cursor;DatabaseName=abc\" user=\"abc\" password=\"abc\"/>");
        System.out.println("\t<document>");
        System.out.println("\t<entity name=\"" + clazz.getSimpleName() + "\" pk=\"" + pkName + "\" query=\" select * from " + clazz.getSimpleName() + "\" ");
        System.out.println("\t\tdeltaQuery=\"select PkOrId from " + clazz.getSimpleName() + " where crete_time > '${dataimporter.last_index_time}'\">");
        System.out.println(sb.toString());
        System.out.println("\t\t</entity>");
        System.out.println("\t</document>");
        System.out.println("</dataConfig>");
        System.out.println("\n\n");
        System.out.println("=================增加配置[managed-schema.xml] 映射==============\n");
        for (Field field : fields) {
            System.out.print("\t<field name=\"" + field.getName() + "\" type=\"");
            //System.out.println(field.getType().toString());
            switch (field.getType().getSimpleName()) {
                case "String":
                    System.out.print("string");
                    break;
                case "int":
                case "Integer":
                    System.out.print("pint");
                    break;
                case "BigDecimal":
                case "Double":
                case "double":
                    System.out.print("pdouble");
                    break;
            }
            System.out.println("\" indexed=\"true\" stored=\"true\"/>");
        }

        System.out.println("\n\n=================增加配置[solrconfig.xml]最后的 </config> 内 ==============\n");
        System.out.println("\t<requestHandler name=\"/dataimport\" class=\"org.apache.solr.handler.dataimport.DataImportHandler\">\n" +
                "\t\t<lst name=\"defaults\">\n" +
                "\t\t<str name=\"config\">data-config.xml</str>\n" +
                "\t\t</lst>\n" +
                "\t</requestHandler>");
        System.out.println("\n\t<uniqueKey>设置主键[" + pkName + "],请注意默认主键名字id，一个文件只能有一个主键,多设置了也无效，不会覆盖</uniqueKey>");
        System.out.println("\n\n");
    }

    private static String fieldHandle(String field) {
        //如果没有大写不用转换直接返回
        Pattern p = Pattern.compile("[A-Z]");
        Matcher m = p.matcher(field);
        if (!m.find()) return "\t\t\t<field name =\"" + field + "\" column=\"" + field + "\"/>\n";

        StringBuilder sb = new StringBuilder().append("\t\t\t<field name =\"").append(field).append("\" column=\"");
        for (int i = 0; i < field.length(); i++) {
            if (Character.isUpperCase(field.charAt(i))) {
                sb.append("_").append(String.valueOf(field.charAt(i)).toLowerCase());
            } else {
                sb.append(field.charAt(i));
            }
        }
        return sb.append("\"/>").append("\n").toString();
    }

    public static void main(String[] args) {
        Generation(Order.class, "oid");
    }
}
